package com.ponhu.ea2.goodsCenter.provider.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ponhu.ea2.constants.CommonConstant;
import com.ponhu.ea2.dto.LogsForTextAddDto;
import com.ponhu.ea2.entity.Goods;
import com.ponhu.ea2.entity.GoodsInventoryMd;
import com.ponhu.ea2.entity.Inventory;
import com.ponhu.ea2.enums.SyncAppletTypeEnum;
import com.ponhu.ea2.goodsCenter.provider.biz.OperationRecordLogsCoreService;
import com.ponhu.ea2.goodsCenter.provider.mapper.GoodsInventoryMdMapper;
import com.ponhu.ea2.goodsCenter.provider.mapper.InventoryMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;

/**
 * @author luyu
 * @create 2021-11-08 20:05
 */
@Slf4j
@Service
public class GoodsInfoManagerService {

    @Resource
    private GoodsService goodsService;

    @Resource
    private InventoryMapper inventoryMapper;

    @Resource
    private GoodsInventoryMdMapper goodsInventoryMdMapper;

    @Resource
    private OperationRecordLogsCoreService operationRecordLogsCoreService;

    @Resource
    PhinventoryToAmqpService phinventoryToAmqpService;

    /**
     * 修改商品上下架状态
     * @param inventoryCode
     * @param goodsStatus
     * @param reason
     * @param operator
     * @param operatorId
     * @return
     */
    public Boolean editGoodsStatus(String inventoryCode,Integer goodsStatus,
                                   String reason,String operator,Long operatorId){
        Boolean bool = false;
        try {
            // 根据货品编码查询货品
            Inventory inventory = inventoryMapper.selectOne(new LambdaQueryWrapper<Inventory>()
                    .eq(Inventory::getInventoryCode, inventoryCode));
            if(ObjectUtils.isEmpty(inventory)){
                return false;
            }
            // 根据货品id查询中间表
            GoodsInventoryMd goodsInventoryMd = goodsInventoryMdMapper.selectOne(new LambdaQueryWrapper<GoodsInventoryMd>()
                    .eq(GoodsInventoryMd::getInventoryId, inventory.getId()));
            if(ObjectUtils.isEmpty(goodsInventoryMd)){
                return false;
            }
           if(!((GoodsInfoManagerService)AopContext.currentProxy())
                   .updateGoodsStatus(goodsStatus,goodsInventoryMd)){
               return false;
           }

            CompletableFuture.runAsync(()->{
                // 根据商品更新状态,记录日志
                LogsForTextAddDto logsForTextAddDto = new LogsForTextAddDto();
                logsForTextAddDto.setTextContent(reason);
                logsForTextAddDto.setOperatorId(operatorId);
                logsForTextAddDto.setOperator(operator);
                logsForTextAddDto.setBusinessTable(CommonConstant.BUSINESS_TYPE_INVENTORY);
                logsForTextAddDto.setRecordId(inventory.getId());
                operationRecordLogsCoreService.addTextAddOperationRecordLogs(logsForTextAddDto);
            });
            // 同步会员小程序
            CompletableFuture.runAsync(() ->{
                phinventoryToAmqpService.pushDataToAmqp(
                        Arrays.asList(inventoryCode),
                        SyncAppletTypeEnum.GOODS_STATUS.getCode());
            });
        } catch (Exception e) {
            e.printStackTrace();
            return bool;
        }
        return bool;
    }

    @Transactional(rollbackFor = Exception.class)
    public Boolean updateGoodsStatus(Integer goodsStatus,GoodsInventoryMd goodsInventoryMd){
        return goodsService.updateById(
                new Goods()
                        .setId(goodsInventoryMd.getGoodsId())
                        .setGoodsStatus(goodsStatus));
    }

}
